from django.db import models
import uuid



class Message(models.Model):
    id = models.CharField(max_length=100, primary_key=True, default=uuid.uuid4)
    user_id = models.CharField(max_length=100)
    text = models.TextField()
    room_id = models.CharField(max_length=100)
    role = models.CharField(max_length=255)
    model = models.CharField(max_length=255)
    status = models.CharField(max_length=100)
    create_time = models.DateTimeField(auto_now_add=True)
    update_time = models.DateTimeField(auto_now=True)
    del_flag = models.IntegerField(default=1)

    def serialize(self):
        return {
            'id': self.id,
            'user_id': self.user_id,
            'text': self.text,
            'room_id': self.room_id,
            'role': self.role,
            'model': self.model,
            'create_time': self.create_time.strftime('%Y-%m-%d %H:%M:%S'),
            'update_time': self.update_time.strftime('%Y-%m-%d %H:%M:%S'),
            'status': self.status,
            'del_flag': self.del_flag
        }

    class Meta:
        db_table = 'message'

    @classmethod
    def create_message(cls, id, user_id, text, room_id, role, model):
        existing_message = cls.objects.filter(id=id).first()
        if existing_message:
            cls.update_message(id, text, model)
        else:
            message = cls(id=id, user_id=user_id, text=text, room_id=room_id, role=role, model=model)
            message.save()

    @classmethod
    def update_message(cls, id, text, model):
        message = cls.objects.filter(id=id).first()
        if message:
            message.text = text
            message.model = model
            message.save()

    @classmethod
    def get_messages_by_user_and_room(cls, user_id=None, room_id=None):
        return cls.objects.filter(user_id=user_id, room_id=room_id, del_flag=1).order_by('create_time')

    @classmethod
    def get_messages_by_room(cls, room_id):
        return cls.objects.filter(room_id=room_id, del_flag=1).order_by('create_time')

    @classmethod
    def delete_message(cls, message):
        message.del_flag = 0
        message.save()

    @classmethod
    def delete_message_by_room_id(cls, room_id):
        messages = cls.objects.filter(room_id=room_id, del_flag=1)
        for message in messages:
            message.del_flag = 0
        cls.objects.bulk_update(messages, ['del_flag'])


class BuildInfo(models.Model):
    id = models.CharField(max_length=100, primary_key=True, default=uuid.uuid1)
    sql_str = models.CharField(max_length=600)
    question = models.TextField()
    create_time = models.DateTimeField(auto_now_add=True)

    class Meta:
        db_table = 'build_model_log'

    def serialize(self):
        return {
            'id': self.id,
            'sql_str': self.sql_str,
            'question': self.question,
            'create_time': self.create_time.strftime('%Y-%m-%d %H:%M:%S')
        }

    @classmethod
    def create_build(cls, sql_str, question):
        # 使用 uuid 生成 ID
        id = str(uuid.uuid4())
        build = cls.objects.create(id=id, sql_str=sql_str, question=question)
        return build.id

    @classmethod
    def get_build_by_id(cls, id):
        try:
            build = cls.objects.get(id=id)
            return build
        except cls.DoesNotExist:
            return None
